package tp.pr2.mv;
import java.util.Scanner;

import tp.pr2.mv.command.CommandInterpreter;
import tp.pr2.mv.command.CommandParser;
import tp.pr2.mv.instruction.Instruction;
import tp.pr2.mv.instruction.InstructionParser;

public class Main {
	private static Instruction instruction=null;
	private static CommandInterpreter command=null;
	private static ProgramMV programaMv;
	public static CPU cpuMv;
	
	/**
	 * @param args
	 */
	
	public static void main(String[] args) {
		// TODO Auto-generated method stub
		cpuMv = new CPU();
		programaMv = new ProgramMV();
		Scanner s = new Scanner(System.in);
		
		//boolean instOk=false;
		boolean instEnd = false;
		boolean commandOk = false;
		// Introduce el programa, PARTE 1
		System.out.println("Introduce el programa fuente ");
		do {
			System.out.print("> ");
			String line = s.nextLine();
			if (!line.equalsIgnoreCase("END")){
				instruction = InstructionParser.parse(line);
				if(instruction != null)
					programaMv.nuevaInst(instruction);
				else
					System.out.println("Error: Instrucción incorrecta");
			}
			else instEnd = true;
		} while(!instEnd);
		
		// Muestra el programa introducido, PARTE 1
		System.out.println("El programa introducido es: ");
		System.out.println(programaMv.toString());
		
		// Comienzo PARTE 2
		cpuMv.loadProgram(programaMv); //Se vuelca el programa al programMV de la CPU
		CommandInterpreter.configureCommandInterpreter(cpuMv);
		do {
			do {
				System.out.print("> ");
				String line = s.nextLine();
				command = CommandParser.parseCommand(line);
				if(command != null)
					commandOk = command.executeCommand();
				else
					System.out.println("No te entiendo. ");
				
			} while(commandOk && !cpuMv.getParado()); 
		}while(!cpuMv.getParado()); //control.
	
		System.out.println("Finaliza el programa.");
	}
}
		
